package com.lcredu.web;

import com.lcredu.entity.Cart;
import com.lcredu.entity.CartItem;
import com.lcredu.entity.Furn;
import com.lcredu.service.FurnService;
import com.lcredu.service.impl.FurnServiceImpl;
import com.lcredu.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 黎长容
 * @date 2025-07-08 15:52
 */
@WebServlet(name = "CartServlet", urlPatterns = "/CartServlet")
public class CartServlet extends BasicServlet {
    private FurnService furnService = new FurnServiceImpl();



    /**
     * 清空购物车
     */
    protected void deleteAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clear();
        }
        response.sendRedirect(request.getHeader("Referer"));
    }


    /**
     * 删除购物车
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = DataUtils.parserInt(request.getParameter("id"), 0);
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null) {
            cart.deleteItem(id);
        }
        response.sendRedirect(request.getHeader("Referer"));
    }

    /**
     * 更新某个CartItem的数量,即更新购物车
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest request, HttpServletResponse response) {
        int id = DataUtils.parserInt(request.getParameter("id"), 0);
        int count = DataUtils.parserInt(request.getParameter("count"), 1);
        System.out.println("id=" + id);
        System.out.println("count=" + count);
        //获取session购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null) {
            cart.updateCount(id, count);
        }
        System.out.println("cart=" + cart);
        //重新回到请求过来的页面
        try {
            response.sendRedirect(request.getHeader("Referer"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    //增加一个添加家居信息到购物车的方法
    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //得到添加家具的id
        int id = DataUtils.parserInt(request.getParameter("id"), 0);
        //获取到id对应的
        //todo 这里待会去写为空的情况
        Furn furn = furnService.getFurn(id);
        if (furn.getStock() > 0) {


            //构建item对象
            CartItem item = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1, furn.getPrice());

            //判断是否有购物车
            Cart cart = (Cart) request.getSession().getAttribute("cart");
            if (cart == null) {
                //如果没有购物车,就新创建一个购物车给session
                cart = new Cart();
                request.getSession().setAttribute("cart", new Cart());
            }

            //如果不为空,就直接添加就好了
            cart.addItem(item);

            System.out.println(cart);

            //添加完成后,返回到添加家居的页面
            String referer = request.getHeader("Referer");
            System.out.println("referer" + referer);
            //返回购物车中的总数
            try {
                response.sendRedirect(referer);
            } catch (IOException e) {
                System.out.println("不知道为什么会报错...");
            }

        } else {
            response.sendRedirect(request.getHeader("Referer"));
        }
    }
}
